/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller.web;

/**
 *
 * @author Hatta
 */
import controller.web.Servlet;
import business.DaftarUser;
import business.User;
import business.Admin;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;
import org.eclipse.persistence.sessions.Session;

public class UserNew extends Servlet {

    public UserNew(){
        super();
        setTemplate("/WEB-INF/UserNew.jsp");
    }

    @Override
    protected void process() {

        setMessage("");

        if (getRequest().getParameter("act") != null && getRequest().getParameter("act").equals("add")) {
            try {
                if (validate_field()) {
                        DaftarUser du = new DaftarUser();
                        String stats = getRequest().getParameter("tp");
                        int stat = Integer.parseInt(stats);
                        {if (stat == 1) {
                            {
                            User u = new User();
                            u.setUsername(getRequest().getParameter("username"));
                            u.setPassword(getRequest().getParameter("password"));
                            u.setEmail(getRequest().getParameter("email"));
                            u.setNama_depan(getRequest().getParameter("nama_depan"));
                            u.setNama_belakang(getRequest().getParameter("nama_belakang"));
                            u.settelepon(getRequest().getParameter("telepon"));
                            u.setalamat(getRequest().getParameter("alamat"));
                            u.setkota(getRequest().getParameter("kota"));
                            u.setprovinsi(getRequest().getParameter("provinsi"));
                            u.setnegara(getRequest().getParameter("negara"));
                            u.setKodePos(getRequest().getParameter("KodePos"));
                            du.tambahUser(u);
                            }
                         }

                        {if (stat == 0) {
                        Admin ad = du.getAdmin(getRequest().getParameter("username"));
                        if (ad == null) {
                            ad = new Admin();
                            ad.setUsername(getRequest().getParameter("username"));
                            ad.setPassword(getRequest().getParameter("password"));
                            du.tambahAdmin(ad);
                                    }
                            }
                        }
                        }
                } else {
                    getResponse().sendRedirect("dataKosong");
                }
                getResponse().sendRedirect("adminHome");
            } catch (IOException ex) {
                Logger.getLogger(UserNew.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }


    boolean validate_field() {

        String username = getRequest().getParameter("username");
        String password = getRequest().getParameter("password");
        if(username == null||password == null){
            return false;
        }

        return true;
    }


}

